package com.zimi.controller;


import com.zimi.model.SysOrgExtend;
import com.zimi.service.SysRolePrivilegeService;
import com.zimi.util.GsonUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 机构功能授权表 api
 *
 * @author lnintr
 * @since 2019-05-26
 */
@RestController
@RequestMapping("sys-role-privilege")
public class SysRolePrivilegeApi {
    @Autowired
    private SysRolePrivilegeService sysRolePrivilegeService;

    @PostMapping("roleOrgList")
    @ApiOperation("查询授权和机构信息")
    public Object selectRoleOrgList(SysOrgExtend record) {
        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("List", sysRolePrivilegeService.getOrglistByRole(record));
        objectMap.put("code", 0);
        return objectMap;
    }

    @PostMapping("saveOrgRole")
    @ApiOperation("修改权限和机构信息")
    public Object saveOrgRole(SysOrgExtend record) {
        sysRolePrivilegeService.saveOrgRole(record, 1);

        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("code", 0);
        return objectMap;
    }

    @PostMapping("deleteOrgRole")
    @ApiOperation("修改权限和机构信息")
    public Object deleteOrgRole(SysOrgExtend record) {
        sysRolePrivilegeService.saveOrgRole(record, 2);

        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("code", 0);
        return objectMap;
    }

    @PostMapping("roleFunctionList")
    @ApiOperation("查询授权和机构信息")
    public Object selectRoleFunctionList(SysOrgExtend record) {
        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("List", sysRolePrivilegeService.getFunctionlistByRole(record));
        objectMap.put("code", 0);
        return objectMap;
    }

    @PostMapping("saveFunctionRole")
    @ApiOperation("修改权限和机构信息")
    public Object saveFunctionRole(HttpServletRequest request) {
        String[] ma = request.getParameterValues("privilageList");

        List<SysOrgExtend> sysOrgExtends = GsonUtil.json2Objs(ma[0], SysOrgExtend.class);

        sysRolePrivilegeService.saveFunctionOrg(sysOrgExtends, 1);

        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("code", 0);
        return objectMap;
    }

    @PostMapping("deleteFunctionRole")
    @ApiOperation("删除权限和机构信息")
    public Object deleteFunctionRole(HttpServletRequest request) {
        String[] ma = request.getParameterValues("privilageList");

        List<SysOrgExtend> sysOrgExtends = GsonUtil.json2Objs(ma[0], SysOrgExtend.class);

        sysRolePrivilegeService.saveFunctionOrg(sysOrgExtends, 2);

        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("code", 0);
        return objectMap;
    }
}
